 /**
 * @author caif
 * @fileoverview document app viewer
 */
;(function(){
	App.Views.appView = Backbone.View.extend({
		el : $("#glodon_content_wrap"),
		events: {
			"click #fnav .folder-btn": "createFolder",
			"click #fnav .upload": "uploadModalShow",
			"click .create-project": "createProject",
			"click .rdeleteAll": "deleteAll",
			"click .delbtn": "deleteSelected",
			"click .downBtn": "downloadSelected",
			"click .pdownBtn": "pdownloadSelected",
			"click .recoverSelect": "recoverSelected",
			"contextmenu .doc-item, .document_list": "itemContenxtMenu",
			"contextmenu .mod-project-list .nav-list-item": "leftContenxtMenu",
			"click .drop-down-icon": "dropDownLeftMenu",
			"click .toggle-panel": "togglePanel",
			"click .toggle-list": "toggleProjectList",
			"dblclick .nav-list-item": "toggleProjectList",
			"click #glodon-app" : "selectApp"
		},
		
		leftMenuIds: {
            ownMe: 'ownMe',
            shareByMe: 'shareByMe',
            publicMe: 'publicMe',
            shareForMe: 'shareForMe',
            recycleMe: 'recycleMe'
		},
		selectApp : function(){
			$("#headerBar").show(200);
			$(document).one('click', function(e){
			    $("#headerBar").hide(200);
			});
			return false;
		},
		
		//current filter data, mainly be assigned in appRouter.js
		currentFilter: {},
		
		//cache project items data
		cacheItemsList: {},
		
		initialize: function(){
			var self = this;
			$("#fnav").html(tpl.headerBtn);
			self.docList = self.$('#document_list');
			self.docViewer = self.$('#document_viewer');
			self.projectList = self.$('#mod-project-list');
			self.projectListLoadingTip = $('#loading-projectlist-tip');
			self.navList = $('#nav-list');
			self.leftSider = $('#sider');
			self.allProjectPanel = $('#my-allproject');
			self.noProjectTip = $('#mod-noproject');
			self.breadcrumb = $('#breadcrumb');
			App.itemHelper.init(self);
			//left menu
			self.leftMenuItemCollection = new App.Collections.ItemCollection;
			self.leftMenuItemCollection.bind('add', self.addLeftMenuByModel, self);
			//project
			self.projectCollection = new App.Collections.ItemCollection;
			self.projectCollection.bind("reset", self.addProject, self);
			self.projectCollection.bind("add", self.addLeftMenuByModel, self);
			App.itemHelper.initLeftMenu();
			//bread
    	    self.breadcrumbCollection = new App.Collections.ItemCollection;
    	    self.breadcrumbCollection.bind("reset", self.resetBreadcrumb, self);
    	    self.breadcrumbCollection.bind("add", self.addBreadcrumbByModel , self);
    	    //item
    	    self.itemCollection = new App.Collections.ItemCollection();
		    self.itemCollection.bind("reset", self.addItems, self);
		    self.itemCollection.bind("add", self.addItemByModel, self);
		    self.itemCollection.bind("remove", self.removeItem, self);
    	    self.contextMenuView = new App.Views.contextMenuView({
    	       model: new App.Models.contextMenuModel,
    	       appView: this
	        });
			self.contextMenu = self.contextMenuView.$el;
			self.leftMenuContextMenuView = new App.Views.leftMenuContextMenuView({
                model: new App.Models.leftMenuContextMenuModel,
                appView: this
			});
			self.leftMenuContextMenu = self.leftMenuContextMenuView.$el;
			//role
            self.RoleCollection = new App.Collections.ItemCollection();
            self.MemberCollection = new App.Collections.ItemCollection();
			App.uploadHelper.init(self);
			self.bindEvent();
		},
		
		bindEvent: function(){
			var self = this;
		    $("#bottom-detail-header .close").click(function(){
		    	self.hideDetailPanel();
			});
		    
		    $("#search-input").keyup(function(e){
		    	if(e.which==13)
		    	{
		    		if( $(this).val().trim() )
		    		{
		    			$('#loading-cover').show();
		    			self.itemCollection.fetch({
			                url:  App.util.contextPath + "/search/file?content=" + $(this).val() + "",
			                success: function(collection, response, options){
			                	$('#loading-cover').hide();
			                	self.breadcrumb.html(App.i18n.get('searchResult'));
			                }
			            });
		    		}
		    		else
		    		{
		    			App.util.showTip("请输入关键字");
		    			App.util.hideTip();
		    		}
		    	}
		    })
			self.saveMode = function(mode){
				var s = store.get('glodon_storage') || {};
				s.viewMode = mode;
				store.set('glodon_storage', s);
			}
			self.getMode = function(){
				var s = store.get('glodon_storage');
				return s && s.viewMode;
			}
			self.isThumbMode = function(){
			     return self.getMode() === 'thumb_model';
			}
			$("#thumb_model, #list_model").click(function(e){
				var t = $(this);
				if(t.hasClass('list_model')){
					self.saveMode('list_model');
				}else{
					self.saveMode('thumb_model');
				}
			    t.parent().find("button").removeClass("active");
			    t.addClass("active");
			    Backbone.history.loadUrl(window.location.hash);
			    self.hideDetailPanel();
			});
			
			if(self.isThumbMode()){
				$('#thumb_model').addClass('active');
			}else{
				$('#list_model').addClass('active');
			}
    	    var fancyTooltip = $('#fancy-tooltip');
			fancyTooltip.find(".options").hover(function(){
		        $(this).addClass("highlighted");
		    },function(){
		        $(this).removeClass("highlighted");
		    });
			$("#quota-drop-down").click(function(e){
				fancyTooltip.show();
				fancyTooltip.find('.usedspace').text(App.util.formatSize(App.quota.used));
		        $(document).one('click.fancytooltip', function(e){
				    fancyTooltip.hide(200);
				});
				return false;
			});
			fancyTooltip.find(".feedback").click(function(){
                $("#feedback_dlg").on('show', function(){
                    $(this).find('iframe').contents().find('#title').focus();
                }).modal({
                    keyboard: true,
                    show:true
                });
                fancyTooltip.hide(200);
                return false;
            });
			
            $(document).contextmenu(function(){
                return false;
            });
			App.itemHelper.addShortCutKey();
			return false;
		},
		
		bindDragAndDrop: function(){
            var self = this;
            App.itemHelper.bindDrag();
            App.itemHelper.bindDrop();
		},
		
		moveItems: function(parentId, itemIds){
            var self = this;
            $.ajax({ 
                data: {
                	operate:'move',
                    parentId: parentId,
                    id: itemIds
                },
                type: "put",
                url: App.util.contextPath + "/file.json",
                success: function(data){
                	//always clear cutIds
                	self.contextMenuView.clearCutIds();
                	self.moveItemsByData(data.successItems, parentId);
                	//move failed, e.g., item('s name) existed(main reason)
                	self.showErrorTip(data.failedItems, App.i18n.get('moveFailedTip'));
                }
            });
		},
		
		moveItemsByData: function(data, targetId){
            var self = this,
            currentParentId = self.getParentId();
            if(data.length < 1) return self;
            if(currentParentId === targetId){
                //add items to current view, paste action
            	App.itemHelper.removeSelectedItems();
            	_.each(data, function(item){
                    if(item.folder){
                        self.addFolderByRawData(item, true, false);
                    }else{
                        self.addFileByRawData(item, true);
                    }
                });
                self.updateHeaderBtn();
            }else{
                App.itemHelper.removeItemsByData(data);
            }
            return self;
		},
		
		uploadFiles: function(files){
			$('#mod-plupload').pluploadQueue().addFiles(files);
		},
		
		resetBreadcrumb: function(collection , data){
			this.breadcrumb.empty();
        },
        
        addBreadcrumbByModel: function(model, collection, options){
        	var self = this,
            view = new App.Views.breadcrumbView({
                model: model
            });
            self.breadcrumb.append(view.render().$el);
        },
        
        dealDocumentHeader: function(){
        	var self = this;
        	if(self.isThumbMode()){
        		$("#row-header").hide();
        		$('#thumbnail-view-sort').show().find('.dropdown-menu').append($('#orderheader-wrap')).end().mouseenter(function(){
        			$(this).removeClass('normal-btn-group');
        		}).mouseleave(function(){
        			$(this).addClass('normal-btn-group');
        		});
		        self.docList.addClass('thumbnail-model-view').empty();
        	}else{
        		$("#row-header").show().append($('#orderheader-wrap'));
        		$('#thumbnail-view-sort').hide();
		        self.docList.removeClass('thumbnail-model-view').empty();
        	}
        },
        
        addUploadFile: function(data){
        	data = $.parseJSON(data.response);
        	var self = this;
        	fileData = data.items;
        	//user maybe not in the current directory anymore, simple return
        	fileData.uiParentId = fileData.parentId;
        	if(fileData.parentId === fileData.ownerId) fileData.uiParentId = '';
        	if(fileData.uiParentId !== self.getParentId()) return;
        	if(fileData.folder){
        	    //maybe folder, e.g.: drag folder or upload folder in chrome
        		self.addFolderByRawData(fileData, false, false)
        	}else{
        	   self.addFileByRawData(fileData, false);
        	}
        	//update used quota
        	App.quota.used += fileData.size;
        	App.uploadHelper.updateUploaderMaxFileSize();
        },
                
        addFileByRawData: function(fileData, selected){
            var self = this;
            var existItemModel = self.getItemModelById(fileData.id);
            if(existItemModel){
            	var model = new App.Models.itemModel(fileData);
            	App.util.updateModel(model);
            	existItemModel.set(model.toJSON());
            }else{
            	var itemModel = new App.Models.itemModel(fileData);
                App.util.updateModel(itemModel);
                self.itemCollection.add(itemModel, {selected: selected});
            }
            return self;
        },
        
        addFileByModel : function( model, collection, options ){
            var self = this, 
            view;
            model.fromOwnMe = true;
            var idWithPrefix = App.util.getIdWithPrefix2(model.get('id'));
            if(self.isThumbMode()){
                view = new App.Views.thumbnailView({
                    model: model,
                    id: idWithPrefix
                });
            }else{
                view = new App.Views.fileView({
                    model: model,
                    id: idWithPrefix
                });
            }
            self.removeBlankListTip();
            self.docList.prepend( view.render().el );
            if(options.selected){
                view.render().$el.addClass('highlight');
            }
            self.updateLeftMenuBadge();
            return self;
        },
		
		addBlankListTip: function(tip){
			var self = this;
			self.docList.empty().append($('<div>', {id: 'mod-blank-list', 'class': 'mod-blank-list', text: tip}));
		},
		
		removeBlankListTip: function(){
			$('#mod-blank-list').remove();
		},
		
		addItems: function(collection, response){
		    var self = this,
        	isThumbMode = self.isThumbMode();
		    self.dealDocumentHeader();
    		collection.each(function(model){
		        var view;
                model.set(self.currentFilter, {silent: true});
		        App.util.updateModel(model);
		        var idWithPrefix = App.util.getIdWithPrefix2(model.get('id'));
		        if(isThumbMode){
                    view = new App.Views.thumbnailView({
                        model: model,
                        id: idWithPrefix
                    });
		        }else{
                    if(response.fromRecycle){
                        view = new App.Views.recycleView({
                            model: model,
                            id: idWithPrefix
                        });
                    }else{
                        if(model.get("folder")){
                            view = new App.Views.folderView({
                        	   model: model,
                        	   id: idWithPrefix
                    	    });
                        }else{
                            view = new App.Views.fileView({ 
                                model: model,
                                id: idWithPrefix
                            });
                        }
                    }
		        }
		        self.docList.append(view.render().el);
    		});
            if(response.fromProject){
            	if(collection.length === 0){
                    self.addBlankListTip(App.i18n.get('noItemOfProjectTip'));
                }
            }else{
                self.updateLeftMenuBadge();
            }
    		if(response.fromOwnMe){
                self.bindDragAndDrop();
    		}
		},
		
		addItemByModel: function(model, collection, options){
            var self = this;
            if(model.get('folder')){
                self.addFolderByModel(model, collection, options);
            }else{
                self.addFileByModel(model, collection, options);
            }
            self.updateLeftMenuBadge();
            self.bindDragAndDrop()
		},
		
		addFolderByModel: function(model, collection, options){
		    var self = this, 
		    view = self.addFolderView(model);
		    if(options.renameFolder !== false){
                view.$el.find('.inner-item').trigger('click');
                App.itemHelper.renameItem();
			}
			if(options.selected){
                view.$el.addClass('highlight');
			}
			return self;
		},
		
		addFolderView: function(model){
            var self = this,
            folderView;
            App.util.updateModel(model);
            model.fromOwnMe = true;
            var idWithPrefix = App.util.getIdWithPrefix2(model.get('id'));
            if(self.isThumbMode()){
                folderView = new App.Views.thumbnailView({
                    model: model,
                    id: idWithPrefix
                });
            }else{
                folderView = new App.Views.folderView({
                    model: model,
                    id: idWithPrefix
                });
            }
            if(model.get('projectId')){
                //create folder in project, update left menu
                self.updateProjectItemById(model.get('parentId'));
            	self.expandProjectListByData([model.toJSON()]);
            }
            self.removeBlankListTip();
            self.docList.prepend( folderView.render().el );
            return folderView;
		},
		
		addFolderByRawData: function(folderData, selected, renameFolder){
            var self = this,
            itemModel = new App.Models.itemModel(folderData);
            itemModel.fileClass = "icon_folder";
            self.itemCollection.add(itemModel, {renameFolder: renameFolder, selected: selected})
            return self;
		},
		
		removeItem: function(model, collection, options){
            var self = this;
            if(self.getProjectItemModelById(model.get('parentId'))){
            	//delete item in project, update left menu
                self.projectCollection.remove(model);
                self.updateProjectItemById(model.get('parentId'));
            }else{
                self.updateLeftMenuBadge();
            }
		},
		
		createFolder: function(e){
			var self = this;
			App.util.showTip(App.i18n.get('creatingFolderTip'));
			self.itemCollection.create({
				parentId: self.getParentId()
			}, {
				url: App.util.contextPath + "/file.json",
                wait: true,
			    success: function(model){
			         App.util.hideTip();
			    }
			});
            return false;
		},
		
		createProject: function(e){
            var self = this;
            self.expandPanel(self.allProjectPanel);
            App.modalHelper.showNormalModal({
                title: 'createProject',
                url: App.util.contextPath + '/js/widget/createproject.widget',
                done: function(data, modalBody, okBtn, modal){
                    modalBody.empty().html(Mustache.to_html(data, {
                        projectName: App.i18n.get('projectName'),
                        projectNamePH: App.i18n.get('projectNamePH'),
                        projectDescription: App.i18n.get('description'),
                        projectDescriptionPH: App.i18n.get('projectDescriptionPH')
                    }));
                    var name = modalBody.find('.create-project-name').keydown(function(e){
                        if(e.which === 13){
                            submit();
                        }
                    }),
                    description = modalBody.find('.create-project-description'),
                    errorMsg = modal.find('.error-msg'),
                    submit = function(){
                        if(okBtn.hasClass('disabled')) return;
                    	name.val(name.val());
                    	if(name.val() === ''){
                    	   errorMsg.text(App.i18n.get('projectNamePH'));
                    	   name.focus();
                    	   return;
                    	}
                        okBtn.addClass('disabled');
                    	App.util.showTip(App.i18n.get('isCreatingProject'));
                		self.projectCollection.create({
                		    name: name.val(),
                            description: description.val()
                		}, {
                		    url: App.util.contextPath + '/project?file',
                		    success: function(data){
                                App.util.hideTip();
                                modal.modal('hide');
                                okBtn.removeClass('disabled');
                            },
                            error: function(){
                                okBtn.removeClass('disabled');
                            },
                            fromProjectList: true,
                            wait: true
                		})
                    }
                    okBtn.click(submit);
                }
            });
            return false;
		},
		
		addProject: function(collection, response){
            var self = this;
            self.projectListLoadingTip.fadeOut(function(){
                self.projectList.empty().hide().slideDown();
                self.allProjectPanel.addClass('toggle-panel-expand');
                if(collection.length > 0){
                    collection.each(function(model){
                        self.addLeftMenuByModel(model, collection, response);
                    });
                    self.isProjectListInsertToDOM = true;
                    if(response.activeMenu){
                        App.router.activeProjectMenu(true);
                    }
                }else{
                    self.noProjectTip.fadeIn();
                }
                return self;
            });
		},
				
		addLeftMenuByModel: function(model, collection, options){
            var self = this,
            id = model.get('id'),
            idWithPrefix = App.util.getIdWithPrefix(id);
            if(options.fromProjectList){
            	var view = new App.Views.projectView({
                    model: model,
                    id: idWithPrefix
                });
                var el = view.render().$el;
                self.noProjectTip.hide();
                if(self.newMenuUL){
                    self.newMenuUL.append(el);
                }else{
                    self.projectList.append(el);
                    self.isProjectListInsertToDOM = true;
                }
            }else{
            	var view = new App.Views.leftMenuItemView({
                    model: model,
                    id: idWithPrefix
                });
                self.navList.append(view.render().$el);
            }
            return self;
		},
		
		addProjectByRawData: function(data){
            var self = this,
            projectModel = new App.Models.projectModel(data);
            self.projectCollection.add(projectModel, {fromProjectList: true});
            return self;
		},
		
        itemContenxtMenu: function(e){
        	var self = this;
            if(!$(e.target).is(':input')){
                self.contextMenuView.contextMenu(e);
                return false;
            }
        },
        
        leftContenxtMenu: function(e){
            var self = this,
            currentTarget = $(e.currentTarget);
            if(currentTarget.parent().is('.sub-nav-list')) return false;//todo
            currentTarget.find('a')[0].click();
            self.leftMenuContextMenuView.contextMenu($(e.currentTarget), e);
            return false;
        },
        
        dropDownLeftMenu: function(e){
            var self = this;
            self.leftMenuContextMenuView.contextMenu($(e.currentTarget).closest('li'), e);
            return false;
        },
        
        togglePanel: function(e){
            var self = this,
            t = $(e.currentTarget);
            if(t.is('.toggle-panel-expand')){
                self.collapsePanel(t);
            }else{
                self.expandPanel(t);
            }
        },
        
        expandPanel: function(panel){
            var self = this;
            panel.addClass('toggle-panel-expand');
            if(panel.is('.my-allitems')){
                panel.next().slideDown();
            }else if(panel.is('.my-allproject')){
                App.router.fetchProjectList(window.location.hash, false);
            }
        },
        
        collapsePanel: function(panel){
            var self = this;
            panel.removeClass('toggle-panel-expand');
            if(panel.is('.my-allproject') && panel.next().children().length === 0){
                self.noProjectTip.slideUp();
            }else{
                panel.next().slideUp();
            }
        },
        
        toggleProjectList: function(e){
            var self = this,
            t = $(e.currentTarget),
            menu = t.closest('.nav-list-item'),
            model = self.getProjectItemModelById(menu.attr('id')),
            projectId = model.get('projectId'),
            id = model.get('id');
            if(model.get('uiOpen')){
                model.set('uiOpen', false);
            }else{
                if(self.cacheItemsList[id]){
                	model.set('uiOpen', true);
                }else{
                    App.router.staticFetchItems(id, projectId, function(data, itemId, pId){
                        self.cacheItemsList[itemId] = data;
                        self.expandProjectListByData(data);
                    });
                }
            }
            return false;
        },
        
        expandProjectListByData: function(data, callback){
            var self = this;
            _.each(data, function(itemData){
            	if(itemData.folder){
                    var parent = $('#' + App.util.getIdWithPrefix(itemData.parentId));
                	if(parent.length > 0){
                		self.getProjectItemModelById(itemData.parentId).set('uiOpen', true);
                    }
                    if(parent.find('> ul').length){
                    	self.newMenuUL = parent.find('> ul');
                    }else{
                        self.newMenuUL = $('<ul>', {
                            'class': 'nav-list sub-nav-list'
                        });
                        parent.append(self.newMenuUL.fadeIn());
                    }
                    self.addProjectByRawData(itemData);
                    if(itemData.files !== null){
                        self.expandProjectListByData(itemData.files);
                    }
                }
            });
            callback && callback();
        },
		
		uploadModalShow: function(e){
            $('#mod-plupload').modal('show');
            return false;
		},
		
		deleteAll: function(e){
			return App.itemHelper.deleteAll(e);
    	},
        
        deleteSelected: function(){
            var self = this;
            self.contextMenu.find('.deleteItem').trigger('click');
            return false;
        },
    	
    	downloadSelected: function(){
		    var self = this;
            self.contextMenu.find('.downloadItem').trigger('click');
			return false;
    	},
    	
    	pdownloadSelected : function(){
    	    var self = this;
            self.contextMenu.find('.pdownloadItem').trigger('click');
			return false;
    	},
    	
    	recoverSelected : function(){
    		var self = this;
            self.contextMenu.find('.recoverItem').trigger('click');
		    return false;
    	},
    	
    	getActionParam: function(){
    		var self = this;
    		var param = {
    			items: self.docList.find('.highlight')
    		}
    		if(param.items.length > 1){
    			param.multi = true;
    		}else if(param.items.length === 1){
    			param.multi = false;
    		}
            _.extend(param, self.currentFilter);
    		if(param.items.length > 0){
	    		var model = self.getItemModelById(param.items.first().attr('id'));
	    		if(param.items.length === 1){
	    			param.isFolder = model.get('folder');
	    		}
	    		param.shared = model.get('shared');
	    		param.published = model.get('published');
	    		param.inComplete = model.get('inComplete');
	    		param.lock = model.get('lock');
	    		if(param.fromProject){
	    		    param.privileges = self.getUIPrivilegesByData(model.get('attributes').privileges);
	    		}
    		}
    		return param;
    	},
    	
    	getUIPrivilegesByData: function(data){
    	   var self = this,
    	   uiPrivileges = {};
    	   if(data.indexOf('w') !== -1 || data.indexOf('i-w') !== -1){
    	       uiPrivileges.canWrite = true;
    	   }
    	   if(data.indexOf('d') !== -1 || data.indexOf('i-d') !== -1){
               uiPrivileges.canDelete = true;
           }
           if(data.indexOf('r') !== -1 || data.indexOf('i-r') !== -1){
               uiPrivileges.canRead = true;
           }
           return uiPrivileges;
    	},
    	
		updateHeaderBtn: function( model ){
			var self = this,
			nav = $('#fnav'),
			actionParam = self.getActionParam();
		    nav.find('li').hide();
		    if(actionParam.items.length === 0){
		    	if(actionParam.fromOwnMe){
		    		nav.find('.header_bme').show();
		    	}else if(actionParam.fromRecycle){
		    	    if(self.docList.children(':not(.mod-blank-list)').length !== 0){
		    	        nav.find('.header_recycle_empty').show();
		    	    }
		    	}else if(actionParam.fromProject){
                    nav.find('.header_bme').show();
		    	}
		    }else{
			    if(actionParam.fromRecycle){
			    	nav.find('.header_recycle').show();
			    }else if(actionParam.fromOwnMe){
			    	nav.find('.header_bme, .header_delete').show();
			    	if(actionParam.multi || actionParam.isFolder){
			    		nav.find('.header_downloadzip').show();
			    	}else{
			    	    if( model ){
			    	        App.itemHelper.getDownloadOption( model , nav);
			    	    }else{
			    	        nav.find('.header_download').show();
			    	    }
			    	}
			    }else if(actionParam.fromProject){
                    nav.find('.header_bme').show();
                }else if(actionParam.fromFilesByMe || actionParam.fromFilesForMe || actionParam.fromPublic){
			    	if(actionParam.multi || actionParam.isFolder){
                        nav.find('.header_downloadzip').show();
                    }else{
    			    	nav.find('.header_download').show();
                    }
			    }
		    }
		    return self;
		},
		
		hideDetailPanel: function(){
			var self = this;
			
		    $("#bottom-detail-wrap").animate({
		    	right: -300
		    },function(){
		    	self.docViewer.removeClass('expand-detail');
		    });
		    return self;
		},
		
		showDetailPanel: function(){
			var self = this;
			
		    $("#bottom-detail-wrap").animate({
		    	right: 0
		    },function(){
		    	self.docViewer.addClass('expand-detail');
		    });
		    return self;
		},
		
		toggleDetailPanel: function(){
			var self = this;
			if(self.isDetailPanelShown()){
				self.hideDetail();
			}else{
				self.showDetail();
			}
			return self;
		},
		
		isDetailPanelShown: function(){
            return this.docViewer.hasClass('expand-detail');
		},
		
		getParentId: function(){
            return this.currentFilter.parentId;
		},
		
		getProjectId: function(){
			var self = this,
			id = self.getParentId(),
			projectItemModel = self.getProjectItemModelById(id);
            return projectItemModel.get('projectId');
        },
        
        getRootPath: function(){
            return this.currentFilter.rootPath;
        },
		
        showErrorTip: function(data, tipText){
        	if(!data || data.length < 1) return this;
            var self = this,
            failedFileCon = $('<div>', {'class': 'failed-file-list'});
            for (var i = 0; i < data.length; i++) {
                failedFileCon.append($('<p>', {'class': 'failed-file', title: data[i], text: data[i]}));
            }
            $("#dialog").html(tpl.errorDialog);
            $('#glodon_error_dlg').modal('show').find('.modal-body')
            .append($('<div>', {'class': 'failed-file-tip', text: tipText})).append(failedFileCon);
            return self;
        },
        
        getItemModelById: function(id){
            var self = this;
            return self.itemCollection.get(App.util.removePrefixOfId(id));
        },
        
        getProjectItemModelById: function(id){
            var self = this;
            return self.projectCollection.get(App.util.removePrefixOfId(id));
        },
        
        getProjectItemInfoById: function(id){
        	id = App.util.removePrefixOfId(id);
            var self = this,
            count = 0,
            hasFolder = false;
            self.itemCollection.each(function(model){
                if(model.get('parentId') === id){
                    count++;
                }
                if(model.get('folder') === true){
                    hasFolder = true;
                }
            });
            return {
                count: count,
                hasFolder: hasFolder
            }
        },
        
        updateProjectItemById: function(id){
            var self = this,
            itemInfo = self.getProjectItemInfoById(id),
            projectItemModel = self.getProjectItemModelById(id);
            projectItemModel.set({
                count: itemInfo.count,
                hasFolder: itemInfo.hasFolder
            });
            if(itemInfo.count === 0){
                self.addBlankListTip(App.i18n.get('noItemTip'));
            }
        },
        
        getRoleItemModelById: function(id){
            var self = this;
            return self.RoleCollection.get(App.util.removePrefixOfId(id));
        },
        
        getMemberItemModelById: function(id){
            var self = this;
            return self.MemberCollection.get(App.util.removePrefixOfId(id));
        },
        
        canSetPrivilegeByItemId: function(itemId){
            var self = this,
            itemModel = self.getItemModelById(itemId),
            projectId = itemModel.get('projectId');
            projectModel = null;
            self.projectCollection.some(function(model){
               if(model.get('projectId') === projectId){
                    projectModel = model;
                    return true;
               }
               return false;
            });
            //currently, only the project's administrator can set privilege
            return projectModel.get('attributes').privileges.length === 4;
        },
        
        getActiveLeftMenuId: function(){
            return this.leftSider.find('.active').attr('id');
        },
        
        updateLeftMenuBadge: function(){
            var self = this,
            count = self.itemCollection.length,
            id = self.getActiveLeftMenuId();
            id = App.util.removePrefixOfId(id);
            var menuModel = (self.leftMenuItemCollection.get(id) || self.projectCollection.get(id)).set('count', count);
            if(count === 0){
                var actionParam = self.getActionParam();
                if(actionParam.fromRecycle){
                    self.addBlankListTip(App.i18n.get('recycleIsCleanTip'));
                }else{
                    self.addBlankListTip(App.i18n.get('noItemTip'));
                }
            }
        },
        
        clearDataWhileRefresh: function(){
            var self = this;
            self.allProjectPanel.data('data-loaded', null);
            self.isProjectListInsertToDOM = false;
            self.cacheItemsList = {};
            self.newMenuUL = null;
            return self;
        }
	});
})();
